package com.hqyj.am.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.am.dao.UserMapper;
import com.hqyj.am.dao.VacationMapper;
import com.hqyj.am.pojo.ResultVo;
import com.hqyj.am.pojo.User;
import com.hqyj.am.pojo.Vacation;
import com.hqyj.am.service.IVacationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 */
@Service
public class VacationServiceImpl extends ServiceImpl<VacationMapper, Vacation> implements IVacationService {

    //注入dao层
    @Resource
    VacationMapper vacationMapper;

    //分页-员工请假/出差,领导查询所有人
    @Override
    public ResultVo queryByPage(Vacation vacation){
        System.out.println(vacation);
        ResultVo rv = new ResultVo();

        //1 创建一个分页对象
        Page<Vacation> page = new Page<>(vacation.getPage(), vacation.getRow());
        //创建条件构造器对象
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
        //根据用户名
        boolean isUserName= StringUtils.isNotEmpty(vacation.getUserName());
        queryWrapper.eq(isUserName,"user_name",vacation.getUserName());
        //根据（未审核-已审核）查询  模糊查询
        boolean isVacationState= null != vacation.getVacationState() && !vacation.getVacationState().equals("");
        queryWrapper.eq(isVacationState,"vacation_state",vacation.getVacationState());
        //根据（用户编号）查询  精准查询
        boolean isVacation = null != vacation.getVacationWay() && !vacation.getVacationWay().equals("");
        queryWrapper.eq(isVacation,"vacation_way",vacation.getVacationWay());

        //2 调用查询分页方法
        //selectPage(分页对象，条件构造器对象)
        Page<Vacation> myPage = vacationMapper.selectPage(page,queryWrapper);
//        vacationMapper.selectOne(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        //当前页集合
        map.put("list",myPage.getRecords());
        //总条数
        map.put("total",myPage.getTotal());
        map.put("totalPage", myPage.getPages());
        rv.setSuccess(true);
        rv.setData(map);
        rv.setInfo("分页查询成功勒！");
        return rv;
    }

    //多表分页查询
    @Override
    public ResultVo pageVacationAll(Vacation vacation) {
        ResultVo rv = new ResultVo();
        //1 创建一个分页对象
        Page<Vacation> page = new Page<>(vacation.getPage(), vacation.getRow());

        //2 调用查询分页方法
        //selectPage(分页对象，条件构造器对象)

        Page<Vacation> myPage = vacationMapper.pageVacationAll(page,vacation.getUserName(),vacation.getDepartmentName(),vacation.getVacationWay(),vacation.getVacationState());
//        vacationMapper.selectOne(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        //当前页集合
        map.put("list",myPage.getRecords());
        //总条数
        map.put("total",myPage.getTotal());
        rv.setSuccess(true);
        rv.setData(map);
        rv.setInfo("分页查询成功勒！");
        return rv;
    }

    //员工添加请假/出差
    @Override
    public ResultVo addVacation(Vacation vacation){
        System.out.println(vacation);
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
        //根据用户编号查询
        vacation.setVacationState("未审核");//默认未审核
        boolean isVacation = null != vacation.getVacationId() && !vacation.getVacationId().equals("");
        queryWrapper.eq(isVacation, "vacation_id", vacation.getVacationId());
        int num = vacationMapper.insert(vacation);
        if(num > 0){
            rv.setSuccess(true);
            rv.setInfo("请假添加成功");
        }else{
            rv.setInfo("请假添加失败");
        }
        return rv;
    }

    //员工-请假/出差修改-未审核时修改
    @Override
    public ResultVo updateVacation(Vacation vacation){
        ResultVo rv = new ResultVo();
        Vacation va = vacationMapper.selectByVacationId(vacation.getVacationId());
        if (va.getVacationState().equals("未审核")) {
            //创建条件构造器对象
            QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
            //根据用户编号查询
            boolean isVacation = null != vacation.getVacationId() && !vacation.getVacationId().equals("");
            queryWrapper.eq(isVacation,"vacation_id",vacation.getVacationId());
            int num = vacationMapper.update(vacation,queryWrapper);
            //前端通过下拉列表默认填写审核中
            if (num >0){
                rv.setSuccess(true);
                rv.setInfo("请假修改成功耶");
            }else{
                rv.setInfo("请假修改失败咯");
            }
            return rv;
        }else{
            rv.setInfo("该申请已被审核，不能再修改");
            return rv;
        }
    }

    //领导审核-已审核-审核结果-未审核原因
    @Override
    public ResultVo updateVacationState(Vacation vacation) {
        ResultVo rv = new ResultVo();
        //查询
        Vacation va = vacationMapper.selectByVacationId(vacation.getVacationId());
        System.out.println("va:" + va);
        if (va.getVacationState().equals("未审核")) {
            va.setVacationState("已审核");
            vacation.setVacationState(va.getVacationState());
                //创建条件构造器对象
                QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
                System.out.println("======" + va);
                //根据用户编号查询
                boolean isVacation = null != vacation.getVacationId() && !vacation.getVacationId().equals("");
                queryWrapper.eq(isVacation, "vacation_id", vacation.getVacationId());

                int num = vacationMapper.update(vacation, queryWrapper);
                //前端通过下拉列表默认填写审核中
                if (num > 0) {
                    rv.setSuccess(true);
                    rv.setInfo("请假修改成功耶");
                } else {
                    rv.setInfo("请假修改失败咯");
                }
                return rv;
            } else {
                rv.setInfo("该申请已被审核，不能再修改");
                return rv;
            }
        }

    //删除请假
    @Override
    public ResultVo deleteVacation(Vacation vacation){
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
        //根据用户编号查询
        boolean isVacation = null != vacation.getVacationId() && !vacation.getVacationId().equals("");
        queryWrapper.eq(isVacation,"vacation_id",vacation.getVacationId());
        int num =  vacationMapper.delete(queryWrapper);
        if (num > 0){
            rv.setSuccess(true);
            rv.setInfo("请假删除成功耶");
        }else{
            rv.setInfo("请假删除失败咯");
        }
        return rv;
    }

    //根据员工姓名查询
    @Override
    public ResultVo selectByUserName(String userName) {
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
        //根据用户编号查询
        boolean isVacation = null != userName && !userName.equals("");
        //精准查询
        queryWrapper.eq(isVacation,"user_name",userName);
        Vacation vacation = vacationMapper.selectByUserName(userName);
        if(vacation != null){
            rv.setSuccess(true);
            rv.setInfo("根据员工姓名查询成功耶");
            rv.setData(vacation);
        }else{
            rv.setInfo("根据员工姓名查询失败咯");
        }
        return rv;
    }

    //根据员工编号查询
    @Override
    public ResultVo selectByVacationId(Integer vacationId) {
        ResultVo rv = new ResultVo();
        //创建条件构造器对象
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<>();
        //根据用户编号查询
        boolean isVacation = null != vacationId && !vacationId.equals("");
        //精准查询
        queryWrapper.eq(isVacation,"vacation_id",vacationId);
        Vacation vacation = vacationMapper.selectByVacationId(vacationId);
        if(vacation != null){
            rv.setSuccess(true);
            rv.setInfo("根据姓名编号查询成功耶");
            rv.setData(vacation);
        }else{
            rv.setInfo("根据姓名编号查询失败咯");
        }
        return rv;
    }

    @Override
    public ResultVo selectByState(String vacationState) {
        ResultVo rv = new ResultVo();
        Vacation va = new Vacation();
        va.setVacationState(vacationState);
        List<Vacation> vacations = vacationMapper.selectByState(vacationState);
        System.out.println("vacations:::"+vacations);
        if(va.getVacationState().equals(vacationState)){
            rv.setSuccess(true);
            rv.setInfo("根据姓名编号查询成功耶");
            rv.setData(vacations);
        }else{
            rv.setInfo("根据姓名编号查询失败咯");
        }
        return rv;
    }

    @Override
    public ResultVo scheduleThing(String vacationState) {
        ResultVo rv = new ResultVo();
        List<Vacation> vacations = vacationMapper.scheduleThing(vacationState);
        rv.setData(vacations);
        rv.setSuccess(true);
        return rv;
    }
}
